#define  _CRT_SECURE_NO_WARNINGS
#include "Queue.h"
#include "tree.h"

QueueNode* BuyNewNode(QDatatype x)
{
	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
	assert(node);
	node->data = x;
	node->next = NULL;
	return node;
}

void InitQueue(Queue* qp)
{
	assert(qp);
	qp->phead = qp->ptail = NULL;
	qp->size = 0;
}

void QueuePush(Queue* qp, QDatatype x)
{
	assert(qp);
	QueueNode* newnode = BuyNewNode(x);
	if (qp->phead == NULL)
	{
		qp->phead = qp->ptail = newnode;
	}
	else
	{
		qp->ptail->next = newnode;
		qp->ptail = newnode;
	}
	qp->size++;
}

bool BoolEmpty(Queue* qp)
{
	assert(qp);
	return qp->phead == NULL;
}

void QueuePop(Queue* qp)
{
	assert(qp);
	assert(!BoolEmpty(qp));

	if (qp->phead == qp->ptail)
	{
		free(qp->phead);
		qp->phead = qp->ptail = NULL;
	}
	else
	{
		QueueNode* tmp = qp->phead->next;
		free(qp->phead);
		qp->phead = tmp;
		
	}
	qp->size--;
}

QDatatype QueueBack(Queue* qp)
{
	assert(qp);
	assert(!BoolEmpty(qp));

	return qp->ptail->data;
}

QDatatype QueueFront(Queue* qp)
{
	assert(qp);
	assert(!BoolEmpty(qp));

	return qp->phead->data;
}

void QueueDestroy(Queue* qp)
{
	assert(qp);
	//assert(!BoolEmpty(qp));
	while (qp->phead )
	{
		QueueNode* tmp = qp->phead->next;
		free(qp->phead);
		qp->phead = tmp;
	}
	qp->phead = qp->ptail = NULL;
}